import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//设置不同的状态码

@WebServlet("/status")
public class StatusServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 约定：浏览器query string中传入形如 type=1 的参数过来
        // type=1,200; type=2,404; type=3,500
        String type = req.getParameter("type");
        if(type.equals("1")) {
            resp.setStatus(200);
        } else if(type.equals("2")) {
            resp.setStatus(404);

            // sendError的效果就是返回一个tomcat自带的错误页面
            resp.sendError(404);
        } else if(type.equals("3")) {
            resp.setStatus(500);
        } else {
            resp.setStatus(504);
        }
    }
}
